﻿using BitButterCORE.Event;
using BitButterCORE.Object;

namespace BitButterCORE.Factory
{
	public class LogEntryFactory : BaseFactory<LogEntry, LogEntryFactory>
	{
		protected LogEntryFactory()
			: base()
		{
			LogInformationEvent.Instance.Handler += OnLogInformation;
			LogWarningEvent.Instance.Handler += OnLogWarning;
			LogErrorEvent.Instance.Handler += OnLogError;
		}

		~LogEntryFactory()
		{
			LogInformationEvent.Instance.Handler += OnLogInformation;
			LogWarningEvent.Instance.Handler += OnLogWarning;
			LogErrorEvent.Instance.Handler += OnLogError;
		}

		void OnLogInformation(IObjectReference<BaseObject> sender, LogEventArgs eventArgs)
		{
			_ = new LogEntry(LogType.INFORMATION, eventArgs.Category, eventArgs.SourceType, eventArgs.SourceID, eventArgs.Message);
		}

		void OnLogWarning(IObjectReference<BaseObject> sender, LogEventArgs eventArgs)
		{
			_ = new LogEntry(LogType.WARNING, eventArgs.Category, eventArgs.SourceType, eventArgs.SourceID, eventArgs.Message);
		}

		void OnLogError(IObjectReference<BaseObject> sender, LogEventArgs eventArgs)
		{
			_ = new LogEntry(LogType.ERROR, eventArgs.Category, eventArgs.SourceType, eventArgs.SourceID, eventArgs.Message);
		}
	}
}
